<!--
 * @Description: 火车行程信息管理
 * @Version: 2.0
 * @Autor: 许国辉
 * @Date: 2022-10-26 08:21:01
 * @LastEditors: 许国辉
 * @LastEditTime: 2023-05-17 14:17:46
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="customHeader"> 导出表格 </a-button>
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref, toRaw } from 'vue';
  import { getTrainJourneyByParams } from '/@/api/travelItinerary/trainItineraryAPI';
  import { Tabs } from 'ant-design-vue';
  import { BasicTable, useTable } from '/@/components/Table';
  import { columns, searchFormSchema } from './trainItineraryData';
  import { JourneyTrainModel } from '/@/api/travelItinerary/model/journey/JourneyTrainModel';
  import { jsonToSheetXlsx } from '/@/components/Excel';
  import dayjs from 'dayjs';

  export default defineComponent({
    components: {
      [Tabs.name]: Tabs,
      [Tabs.TabPane.name]: Tabs.TabPane,
      BasicTable,
    },
    setup() {
      const visible = ref<boolean>(false);
      const journeyTrainModel = ref<JourneyTrainModel>();
      let api = ref(getTrainJourneyByParams);
      let title = ref('普通订单');
      const [registerTable, { reload, getDataSource, setColumns, getForm }] = useTable({
        api,
        pagination: { pageSize: 10 },
        columns: columns,
        formConfig: {
          schemas: searchFormSchema,
          fieldMapToTime: [['createTime', ['createTimeStart', 'createTimeEnd'], 'YYYY-MM-DD']],
        },
        showTableSetting: true,
        tableSetting: { fullScreen: true },
        striped: false,
        useSearchForm: true,
        bordered: true,
        showIndexColumn: false,
      });
      const hideModal = () => {
        visible.value = false;
      };

      function send(record: JourneyTrainModel) {
        // console.log(record);
        journeyTrainModel.value = toRaw(record);
        console.log(journeyTrainModel);

        visible.value = true;
      }

      function handleSuccess() {
        reload();
      }
      async function tabChange() {
        setColumns(columns);
        title.value = '普通订单';
        api.value = getTrainJourneyByParams;
        await getForm().submit;
        reload();
      }
      function customHeader() {
        let header1 = {
          orderId: '订单号',
          bookTime: '预定时间',
          accountNo: '工号',
          bookerName: '预定人',
          fromStation: '出发站',
          arriveStation: '到达站',
          createTime: '创建时间',
          travelId: '审批单号',
          trainCode: '车次',
          seatTypeDes: '席别',
          totalMoney: '总金额',
        };
        interface JourneyData {
          orderId: string;
          bookTime: string;
          accountNo: string;
          bookerName: string;
          fromStation: string;
          arriveStation: string;
          createTime: string;
          travelId: string;
          trainCode: string;
          seatTypeDes: string;
          totalMoney: string;
        }
        let orderArr: JourneyData[] = [];
        for (let k = 0; k < toRaw(getDataSource()).length; k++) {
          let orderdata = ref<JourneyData>({
            orderId: '订单号',
            bookTime: '预定时间',
            accountNo: '工号',
            bookerName: '预定人',
            fromStation: '出发站',
            arriveStation: '到达站',
            createTime: '创建时间',
            travelId: '审批单号',
            trainCode: '车次',
            seatTypeDes: '席别',
            totalMoney: '总金额',
          });
          orderdata.value!.orderId = toRaw(getDataSource())[k].orderId;
          orderdata.value!.bookTime = toRaw(getDataSource())[k].bookTime;
          orderdata.value!.accountNo = toRaw(getDataSource())[k].accountNo;
          orderdata.value!.bookerName = toRaw(getDataSource())[k].bookerName;
          orderdata.value!.fromStation = toRaw(getDataSource())[k].fromStation;
          orderdata.value!.arriveStation = toRaw(getDataSource())[k].arriveStation;
          orderdata.value!.createTime = toRaw(getDataSource())[k].createTime;
          orderdata.value!.travelId = toRaw(getDataSource())[k].travelId;
          orderdata.value!.trainCode = toRaw(getDataSource())[k].trainCode;
          orderdata.value!.seatTypeDes = toRaw(getDataSource())[k].seatTypeDes;
          orderdata.value!.totalMoney = toRaw(getDataSource())[k].totalMoney;

          orderArr.push(orderdata.value);
        }
        jsonToSheetXlsx({
          data: orderArr,
          header: header1,
          filename: '火车行程信息' + dayjs(new Date()).format('YYYY- MM - DD') + '.xlsx',
          json2sheetOpts: {
            // 指定顺序
            // header: ['name', 'id'],
          },
        });
      }
      onMounted(() => {
        //employeeData.initEmployeeData();
      });

      return {
        tabChange,
        registerTable,
        handleSuccess,
        send,
        customHeader,
        visible,
        hideModal,
        journeyTrainModel,
      };
    },
  });
</script>
